package com.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yygh.cmn.listener.ExcelListener;
import com.yygh.cmn.mapper.DictMapper;
import com.yygh.cmn.service.DictService;
import com.yygh.model.cmn.Dict;
import com.yygh.vo.cmn.DictEeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {


    @Override
    //根据数据id查询子数据列表
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    public List<Dict> findChlidData(Long id) {
        LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dict::getParentId,id);
        List<Dict> dicts = baseMapper.selectList(lqw);

        for (Dict dict:dicts){
            Long did = dict.getId();
            boolean flag = checkChild(did);
                dict.setHasChildren(flag);

        }
        return dicts;
    }

    @Override
    public void exportDictData(HttpServletResponse response) {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "dict";
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

        List<Dict> dicts = baseMapper.selectList(null);

        ArrayList<DictEeVo> dictEeVos = new ArrayList<>();
        for (Dict dict1:dicts){
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict1,dictEeVo);
            dictEeVos.add(dictEeVo);
        }
        try {
            EasyExcel.write(response.getOutputStream(),DictEeVo.class).sheet(fileName).doWrite(dictEeVos);
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    @Override
    @CacheEvict(value = "dict", allEntries=true)
    public void importDictData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), DictEeVo.class, new ExcelListener(baseMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    //根据dictcode和value查询
    public String getDictName(String dictCode, String value) {
        if(StringUtils.isEmpty(dictCode)){
            LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Dict::getValue,value);

            Dict dict = baseMapper.selectOne(lqw);
            return dict.getName();
        }else {
            Dict dictBydict_code = this.getDictBydict_code(dictCode);
            long parent_id= dictBydict_code.getId();

            LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Dict::getParentId,parent_id);
            lqw.eq(Dict::getValue,value);

            Dict dict = baseMapper.selectOne(lqw);
            return dict.getName();

        }
    }



    @Override
    public List<Dict> findChildDcode(String dcode) {
        Dict dict = this.getDictBydict_code(dcode);
        Long id = dict.getId();

        List<Dict> chlidData = this.findChlidData(id);


        return chlidData;
    }

    //判断id下面是否有子节点
    private boolean checkChild(Long id){
        LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dict::getParentId,id);
        Integer count = baseMapper.selectCount(lqw);

        return count>0;
    }

    private Dict getDictBydict_code(String dict_code){
        LambdaQueryWrapper<Dict> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dict::getDictCode,dict_code);
        Dict dict = baseMapper.selectOne(lqw);
        return dict;
    }
}
